<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/6/15 0015
 * Time: 10:49
 */

namespace app\api\controller\efficiency;

/*
 * 添加用户接口类
 * */

use think\Request;

class Adduser extends Base
{
    private $sign;
    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        $this->sign = new Sign();
    }

    /*
     * 添加用户
     * */
    public function index(){
        if (request()->isPost()){
            $post = request()->post();
//            签名验签
            $res = $this->sign->verifyUserSignature($post);
            if ($res) return $this->info(true,$res);
            return $this->addUser($post['phone']);
        }
    }
    /*
     * 添加用户
     * */
    private function addUser($mobile){
//        检测用户是否以注册
        if (model('MemInfo')->SelectData(['Mobile'=>$mobile])) return $this->info(true,201);
//        查询来源渠道ID
        $tg = model('TgAdmin')->tgData(['Name'=>'效率花呗'],'ID');
        $TgadminID = !empty($tg)?$tg['ID']:1;
//        添加用户
        $res = model('MemInfo')->addUser([
            'UserName'=>$mobile,
            'Mobile'=>$mobile,
            'TgadminID'=>$TgadminID
        ]);
        if ($res) return $this->info();
        return $this->info(true,500);
    }
}